var app = getApp()
Page({
  data: {
    objarr: [],
    position:[],
    width:0,
    imgheight:0,
    indexdata:{},
    height:0,
    currentTab:1,
    page: 1,
    pagesize: 5,
    totalpage: 0,//最大页数
    nomore: true,
    isdb:false,
    site_info:{},
    site_id:0
  },
  onShareAppMessage: function (res) {
    let member_id = app.globalData.userInfo.member_id
    return {
      title: '成纪先锋微平台',
      path: '/pages/broadcast/broadcast?inviter_id=' + member_id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onLoad: function (options) {
    var that = this;
    console.log(wx.getStorageSync('rd_session'))
    
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        that.setData({
           width: res.windowWidth,
           height:res.windowHeight })
      }
    })
    
    if(options.from == 'db'){
      console.log(options)
      this.setData({
        isdb:true,
        currentTab:2,
        site_id: options.id
      })
      this.getData2()
    }else{
      this.getData1()
    }

    
  },
  getData1(){
    let par = {
      rd_session : wx.getStorageSync("rd_session"),
      page: this.data.page,
      page_size: this.data.page_size,
    }
    app.ajax("broadcast_site", par, (data, err) => {
      if (data.result == 'success') {
        this.setData({
          objarr: data.data.list,
          totalpage: data.data.total_page
        })
      }
    })
  },
  getData2(){
    let par = {
      rd_session: wx.getStorageSync("rd_session"),
      page: this.data.page,
      page_size: this.data.page_size,
      site_id: this.data.site_id
    }
    app.ajax("broadcast_item", par, (data, err) => {
      if (data.result == 'success') {
        this.setData({
          objarr: data.data.list,
          site_info: data.data.site_info,
          totalpage: data.data.total_page
        })
      }
    })
  },
  getData3() {
    let par = {
      rd_session: wx.getStorageSync("rd_session"),
      page: this.data.page,
      page_size: this.data.page_size,
    }
    app.ajax("broadcast_item_hot", par, (data, err) => {
      if (data.result == 'success') {
        this.setData({
          objarr: data.data.list,
          totalpage: data.data.total_page
        })
      }
    })
  },
  clicktab(e) {
    let tab = e.currentTarget.dataset.tab
    if (this.data.currentTab != tab) {
      this.setData({
        currentTab: tab,
        page: 1,
        isdb:false
      })
      if(tab == 1){
        this.getData1()
      }else if(tab == 2){
        this.getData2()
      }else if(tab == 3){
        this.getData3()
      }
      
    }
  },
  //滑动到底部 上拉加载
  scrollBottom: function () {
    this.data.page++
    if (this.data.page > this.data.totalpage) {
      this.setData({
        nomore: false
      })
    } else {
      let params = {
        rd_session: wx.getStorageSync("rd_session"),
        page: this.data.page,
        page_size: this.data.pagesize
      }
      if(this.data.currentTab == 1){
        app.ajax("broadcast_site", params, (data, err) => {
          let arr = this.data.objarr;
          for (let item of data.data.list) {
            arr.push(item);
          }
          this.setData({
            objarr: arr
          })
        })
      }else if(this.data.currentTab == 2){
        app.ajax("broadcast_item", params, (data, err) => {
          let arr = this.data.objarr;
          for (let item of data.data.list) {
            arr.push(item);
          }
          this.setData({
            objarr: arr
          })
        })
      }else if(this.data.currentTab == 3){
        app.ajax("broadcast_item_hot", params, (data, err) => {
          let arr = this.data.objarr;
          for (let item of data.data.list) {
            arr.push(item);
          }
          this.setData({
            objarr: arr
          })
        })
      }
      
    }

  },
  
  imageLoad(e) {
    let that = this
    this.setData({
      imgheight: app.fixImgW(e, that.data.width),
    })
  },

 
  
});